#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int sum = 0;
        for (auto e : nums)
            sum += e;
        if (sum < x)
            return -1;
        //if(sum == x)
            //return nums.size();
        int target = sum - x;
        int ans = -1;
        int left = 0;
        int right = 0;
        int tmp = 0;
        while (right <= nums.size() - 1)
        {
            tmp += nums[right++];
            while (tmp > target && left < right)
            {
                tmp -= nums[left++];
            }
            if (tmp == target)
                ans = max(ans, right - left);
        }
        if (ans == -1)
            return -1;
        return nums.size() - ans;
    }
};